package com.shiguiwu.springmybatis.designpattern.prototype.scene;

import com.shiguiwu.springmybatis.designpattern.prototype.scene.util.Topic;
import com.shiguiwu.springmybatis.designpattern.prototype.scene.util.TopicRandomUtil;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Map;

/**
 * @description: 题库核心类
 * @author: stone
 * @date: Created by 2021/8/18 11:00
 * @version: 1.0.0
 * @pakeage: com.shiguiwu.springmybatis.designpattern.prototype.scene
 */
@Data
@NoArgsConstructor
public class QuestionBank implements Cloneable {


    private String candidate;

    private String number;

    /**
     * 问答题列表
     */
    private ArrayList<Question> answerQuestions = new ArrayList<>();

    /**
     * 选择题
     */
    private ArrayList<Question> choiceQuestions = new ArrayList<>();

    public QuestionBank(String candidate, String number) {
        this.candidate = candidate;
        this.number = number;
    }



    public QuestionBank append(Question question) {
        answerQuestions.add(question);
        return this;
    }

    public QuestionBank appendChoiceQuestions(Question question) {
        choiceQuestions.add(question);
        return this;
    }




    @Override
    protected Object clone() throws CloneNotSupportedException {
        QuestionBank clone = (QuestionBank) super.clone();

        clone.choiceQuestions = (ArrayList<Question>) choiceQuestions.clone();
        clone.answerQuestions = (ArrayList<Question>) answerQuestions.clone();
        Collections.shuffle(clone.choiceQuestions);
        Collections.shuffle(clone.answerQuestions);

        ArrayList<Question> choices = clone.choiceQuestions;
        choices.forEach(c -> {
            ChoiceQuestion c1 = (ChoiceQuestion) c;
            Topic random = TopicRandomUtil.random(c1.getOption(), c1.getKey());
            c1.setOption(random.getOptions());
            c1.setKey(random.getKey());

        });


        return clone;
    }

    @Override
    public String toString() {
        StringBuilder detail = new StringBuilder("考生：" + candidate +
                "\r\n" +
                "考号：" + number + "\r\n" +
                "--------------------------------------------\r\n" +
                "一、选择题" + "\r\n\n");
        for (int idx = 0; idx < choiceQuestions.size(); idx++) {
            ChoiceQuestion question = (ChoiceQuestion) choiceQuestions.get(idx);
            detail.append("第").append(idx + 1).append("题：").append(question.getName()).append("\r\n");
            Map<String, String> option = question.getOption();
            for (String key : option.keySet()) {
                detail.append(key).append("：").append(option.get(key)).append("\r\n");
                ;
            }
            detail.append("答案：").append(question.getKey()).append("\r\n\n");
        }
        detail.append("二、问答题" + "\r\n\n");
        for (int idx = 0; idx < answerQuestions.size(); idx++) {
            AnswerQuestion answerQuestion = (AnswerQuestion) answerQuestions.get(idx);
            detail.append("第").append(idx + 1).append("题：").append(answerQuestion.getName()).append("\r\n");
            detail.append("答案：").append(answerQuestion.getKey()).append("\r\n\n");
        }
        return detail.toString();
    }
}
